﻿// WashData.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include <map>

#include "toadata.h"

using namespace std;

#define SPEED_OF_LIGHT  (299702547.0)     // in m/s in the air

int Stringsplit(const string& str, const char split, vector<string>& res)
{
    if (str == "")
    {
        return 0;
    }
    size_t pos = str.find(split);
    size_t from = 0;
    while (pos != str.npos)
    {
        string temp = str.substr(from, pos - from);
        res.push_back(temp);
        from = pos + 1;
        pos = str.find(split, from);
    }
    if (from < str.length())
    {
        string temp = str.substr(from);
        res.push_back(temp);
    }
    return (int)res.size();
}

bool ParseToa(const string& str, ToaData& tdata)
{
    vector<string> datas;
    int num = Stringsplit(str, ',', datas);
    if (num == 11)
    {
        tdata.txeui = datas[2];
        tdata.seq = atol(datas[3].c_str());
        tdata.rxeui = datas[4];
        tdata.toa = std::stod(datas[5]);
    }
    return num == 11;
}

bool ParseToa(const string& str, ToaDataSeq & tdata)
{
    vector<string> datas;
    int num = Stringsplit(str, ',', datas);
    if (num == 12)
    {
        tdata.txeui = datas[2];
        tdata.seq = atol(datas[3].c_str());
        tdata.rxeui = datas[4];
        tdata.toa = std::stod(datas[5]);
        tdata.pseq = atoi(datas[11].c_str());
    }
    return num == 12;
}

bool readToa(const string & filename, const string & anchor, const string& tag, vector<ToaData> & toa)
{
    ifstream infile(filename);
    if (!infile)
    {
        cout << "cannot open file:" << filename << endl;
        return false;
    }
    string line;
    getline(infile, line);
    while (!infile.eof())
    {
        getline(infile, line);
        ToaData data;
        if (ParseToa(line, data))
        {
            if (data.txeui == tag && data.rxeui == anchor)
            {
                toa.push_back(data);
            }
        }
    }
    infile.close();
    return true;
}

void AlignData(vector<ToaData> & anchor1, vector<ToaData> & anchor2, vector<TdoaData> & res)
{
    int i = 0;
    int j = 0;
    while (i < anchor1.size())
    {
        while (j < anchor2.size())
        {
            if (anchor2[j].seq >= anchor1[i].seq)
            {
                break;
            }
            j++;
        }
        if (anchor1[i].seq < anchor2[j].seq)
        {
            i++;
        }
        else
        {
            cout << "match seq:" << anchor1[i].seq << "," << anchor2[j].seq << endl;
            TdoaData data;
            data.seq = anchor1[i].seq;
            data.tdoa = anchor1[i].toa - anchor2[j].toa;
            res.push_back(data);
            i++;
            j++;
        }
    }
}

void CheckSeq(const string& filename)
{
    ifstream in(filename);
    if (!in)
    {
        cout << "can't open " << filename << endl;
        return;
    }
    string line;
    getline(in, line);
    map<string, vector<ToaData>> tags;
    while (!in.eof())
    {
        getline(in, line);
        ToaData data;
        if (ParseToa(line, data))
        {
            tags[data.txeui].push_back(data);
        }
    }
    for (auto it = tags.begin(); it != tags.end(); it++)
    {
        vector<ToaData>& datas = it->second;
        int i = 0;
        int j = 0;
        while (i < datas.size())
        {
            ToaData& base = datas[i];
            for (j = i + 1; j < datas.size(); j++)
            {
                if (datas[j].seq == base.seq)
                {
                    if (abs(datas[j].toa - base.toa) > 0.1)
                    {
                        cout << "seq:" << base.seq << " txeui:" << base.txeui << " rxeui:" << base.rxeui << " base:" << datas[j].rxeui << endl;
                    }
                }
                else
                {
                    i = j;
                    break;
                }
            }
            if (j == datas.size())
            {
                break;
            }
        }
    }
}

void CheckLoss(const string & filename)
{
    ifstream in(filename);
    if (!in)
    {
        cout << "can't open " << filename << endl;
        return;
    }
    map<string, vector<ToaDataSeq>> anchors;
    string line;
    getline(in, line);
    while (!in.eof())
    {
        getline(in, line);
        ToaDataSeq toa;
        if (ParseToa(line, toa))
        {
            anchors[toa.rxeui].push_back(toa);
        }
    }

    for (auto it = anchors.begin(); it != anchors.end(); it++)
    {
        vector<ToaDataSeq>& datas = it->second;
        auto it2 = datas.begin();
        uint16_t pseq;
        int num = 0;
        if (it2 != datas.end())
        {
            pseq = it2->pseq;
            it2++;
            while (it2 != datas.end())
            {
                uint16_t nextseq = it2->pseq;
                if (nextseq != pseq + 1)
                {
                    num += nextseq - pseq - 1;
                    //cout << "rxeui=" << it2->rxeui << " seq=" << pseq << " nextseq=" << nextseq << endl;
                }
                pseq = nextseq;
                it2++;
            }
        }
        cout << "rxeui:" << it->first << " total=" << it->second.size() << " loss=" << num << endl;
    }
}

int main(int argc, char ** argv)
{
    string filename = "D:\\alexlu\\entrance_toa\\data\\TOA_20240626_144902.csv";

    CheckLoss(filename);
}
